<?php

namespace app\service;

use app\model\CastleLevelConfig;
use app\model\User;
use app\model\UserFactory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

//城堡等级配置服务
class CastleLevelConfigService extends BaseService
{


    /**
     * 获取下一个等级的配置
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function get_config_by_lv($user_id): array
    {
        $user = (new User())->find($user_id);
        //当前等级
        $current_lv = $user->castle_lv + 1;

        $config = (new CastleLevelConfig())->find($current_lv);
        $user_factory_count = 0;
        if ($config->factory) {
            //用_分割$config->factory
            $factory_arr = explode('_', $config->factory);
            $user_factory_count = (new UserFactory())->where('state', '<>', FACTORY_STATE['已购买'])->where('user_id', $user_id)->where('factory_id', $factory_arr[0])->count();
        }

        //我的工人
        $config->my = [
            'worker' => $user->worker,//当前的工人
            'pass' => $user->pass,//通过的关卡
            'factory' => $user_factory_count,//用户的工厂数量
            'wish' => $user->wish ?? 30,//星愿雕像
            'car' => $user->car ?? 0,//宇宙列车
            'coin' => $user->coin ?? 0,//金币
            'role' => 0,//角色数量
            'travel' => 0,//气球旅行次数
            'cards' => 0,//卡牌数量
        ];


        $this->handleModelData($config);
        return $this->returnSuccess($config->toArray());


    }


    /**
     * 升级城堡
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function upgrade($user_id): array
    {
        $user = (new User())->find($user_id);

        //当前等级
        $current_lv = $user->castle_lv + 1;

        $config = (new CastleLevelConfig())->find($current_lv);

        //判断工人
        if ($config->worker) {
            if ($user->worker < $config->worker) {
                return $this->returnFail(0, '工人数量不足');
            }
        }

        //判断通过的关卡
        if ($config->pass) {
            if ($user->pass < $config->pass) {
                return $this->returnFail(0, '通过的关卡不足');
            }
        }

        //判断工厂
        if ($config->factory) {
            //用_分割$config->factory
            $factory_arr = explode('_', $config->factory);
            $user_factory_count = (new UserFactory())->where('state', '<>', FACTORY_STATE['已购买'])->where('user_id', $user_id)->where('factory_id', $factory_arr[0])->count();
            if ($user_factory_count < $factory_arr[1]) {
                return $this->returnFail(0, '工厂数量不足');
            }
        }

        //判断金币
        if ($config->coin) {
            if ($user->coin < $config->coin) {
                return $this->returnFail(0, '金币不足');
            }
            //扣除金币
            $res_user_money = (new UserService())->change_money($user_id, $config->coin, USER_CAPITAL_TYPE['金币'], USER_CAPITAL_STATUS['扣除']);
            if (!$res_user_money['code']) {
                return $res_user_money;
            }

        }

        //判断星愿雕像
        if ($config->wish){
            if ($user->wish ?? 0 < $config->wish) {
                return $this->returnFail(0, '星愿雕像不足');
            }
        }

        //判断气球旅行次数
        if ($config->travel){
            //待定
        }

        //判断卡牌
        if ($config->cards){
            //待定
        }

        //判断角色数量
        if ($config->role){
            //待定
        }

        //判断宇宙列车到达次数
        if ($config->car){
            //待定
        }


        $this->handleModelData($config);
        return $this->returnSuccess($config->toArray());
    }


}